<?php
/**
 * 
 * Language class file
 * 
 * @author		Stijn Van Minnebruggen
 * @version		1.0.0
 * 
 */

class Lang
{
	
	/**
	 * Current selected domain (id)
	 * 
	 * @var		int
	 */
	
	public $domain_id;
	
	
	/**
	 * Current selected domain (full name)
	 * 
	 * @var		string
	 */
	
	public $domain;
	
	
	/**
	 * Current selected domain (short name)
	 * 
	 * @var		string
	 */
	
	public $domain_short;
	
	
	/**
	 * Current selected language (full name)
	 * 
	 * @var		string
	 */
	
	public $name;
	
	
	/**
	 * Current selected language (in short)
	 * 
	 * @var		string
	 */
	
	public $short;
	
	
	/**
	 * Current selected language (id)
	 * 
	 * @var		int
	 */
	
	public $id;
	
	
	/**
	 * System language vars
	 * 
	 * @var		array
	 */
	
	public $_system;
	
	
	/**
	 * Module language vars
	 * 
	 * @var		array
	 */
	
	public $_module;
	
	
	/**
	 * All available domains and their languages
	 * 
	 * @var		array
	 */
	
	private $_data;
	
	
	/**
	 * Default language id
	 * 
	 * @var		int
	 */
	
	public $default_id;
	
	
	/**
	 * Init class
	 * 
	 * @param	string	$var
	 * @return	bool
	 */
	
	public function __construct()
	{
		// required
			global $cosa, $user;
		
		// set language
			if($cosa->system('installed') && $user->isLoggedIn()) {
				$this->_setLang();
			} else {
				$this->short = DEFAULT_LANG;
				$sysLang = ROOT_PATH.'/kernel/lib/lang/'.$this->short.'.php';
				if(file_exists($sysLang)) include $sysLang;
				return false;
			}
		
		// fetch default language id
			$this->default_id = $this->defaultId();
		
		// fetch all domains and languages
			$this->_data = $this->getData();
			return true;
		
	}
	
	
	/**
	 * Set system language
	 * 
	 * @return	void
	 */
	
	private function _setLang()
	{
		// required
			global $cosa, $db, $user;
		
		// get language vars
			$getL = (isset($_GET['l']) && !empty($_GET['l'])) ? $_GET['l'] : false;
			$sesL = (isset($_SESSION['l']) && !empty($_SESSION['l'])) ? $_SESSION['l'] : false;
			$usrL = (isset($user->language_id) && !empty($user->language_id)) ? $user->language_id : false;
		
		// check vars
			if(!$getL && !$sesL && !$usrL) $l = $this->getLanguageData();
			if(!$getL && !$sesL && $usrL && $user->hasLangPermission($usrL)) $l = $this->getLanguageData($usrL);
			if(!$getL && $sesL && $user->hasLangPermission($sesL)) $l = $this->getLanguageData($sesL);
			if($getL) $l = $this->getLanguageData($getL);
		
		// safety check
			if(!isset($l['id'])) $l = $this->getLanguageData();
		
		// set session
			$_SESSION['l'] = $l['id'];
		
		// set vars
			$this->name = stripslashes($l['name']);
			$this->short = stripslashes($l['short']);
			$this->id = stripslashes($l['id']);
			$this->domain = stripslashes($l['domain']);
			$this->domain_id = stripslashes($l['domain_id']);
			$this->domain_short = stripslashes($l['domain_short']);
		
		// get system language vars
			$sysLang = ROOT_PATH.'/kernel/lib/lang/'.$user->language.'.php';
			if(file_exists($sysLang)) include $sysLang;
		
	}
	
	
	/**
	 * Get all language data from short
	 * 
	 * @param	int			$id
	 * @return	array
	 */
	
	public function getLanguageData($id=false)
	{
		// required
			global $cosa, $db, $user;
		
		// get default language id
			if(!$id) {
				if($user->isLoggedIn()) $id = $user->language_id;
				else $id = DEFAULT_LANG;
			}
		
		// get data
			$sql = "SELECT d.id AS domain_id, d.name AS domain, d.short AS domain_short, l.id, l.short, l.name
					FROM ".$db->table('domains')." d, ".$db->table('languages')." l
					WHERE l.domain_id = d.id
					AND l.id = '".addslashes($id)."'
					";
		
		// fetch data
			$res = mysql_query($sql) or $cosa->kill('Could not fetch language data.', 'db');
			if(!mysql_num_rows($res)) return array();
			return mysql_fetch_array($res, MYSQL_ASSOC);
		
	}
	
	
	/**
	 * Get all the available domains and their languages
	 * 
	 * @param	bool		$active
	 * @param	bool		$checkPermission
	 * @return	array
	 */
	
	public function getData($active=true, $checkPermission=false)
	{
		// required
			global $cosa, $db, $user;
			$data = array();
		
		// create domains query
			$sql = "SELECT id, name, short, active, `default` FROM ".$db->table('domains');
			if($active) $sql .= " WHERE active = '1'";
			$sql .= " ORDER BY name";
		
		// get domains
			$res = mysql_query($sql) or $cosa->kill('Could not fetch domains', 'db');
			while($domain = mysql_fetch_array($res, MYSQL_ASSOC)) {
				
				// set domain data array
					$dmdata = $domain;
					$dmdata['languages'] = array();
					$hasPermission = false;
				
				// create languages query
					$lsql = "SELECT id, name, short, active, `default` FROM ".$db->table('languages')." WHERE domain_id = '".addslashes($domain['id'])."'";
					if($active) $lsql .= " AND active = '1'";
					$lsql .= " ORDER BY name";
				
				// get languages
					$lres = mysql_query($lsql) or $cosa->kill('Could not fetch domain languages.', 'db');
					while($language = mysql_fetch_array($lres, MYSQL_ASSOC)) {
						
						if(!$checkPermission || ($checkPermission && $user->hasLangPermission($language['id']))) {
							
							$dmdata['languages'][] = $language;
							$hasPermission = true;
							
						}
					}
				
				// add dmdata to domain data
					if(!$checkPermission || ($checkPermission && $hasPermission)) $data[$dmdata['id']] = $dmdata;
				
			}
		
		// return data
			return $data;
		
	}
	
	
	/**
	 * Print domains and languages dropdown
	 * 
	 * @return	void
	 */
	
	public function printDropdowns()
	{
		// get data
			global $cosa, $m, $s;
			$this->_data = $this->getData(false, true);
		
		// check number of languages
			$langs = 0;
			foreach($this->_data as $k => $domain) {
				foreach($domain['languages'] as $lk => $lv) $langs++;
			}
		
		// if more languages, continue
			if($langs > 1) {
				
				// create domains dropdown
					echo '<select id="d_dd" name="d" onchange="updateLanguageDropdown(\'l_dd\', this.value);">'."\n\t";
					foreach($this->_data as $k => $domain) {
						$selected = ($this->domain == $domain['name']) ? ' selected="selected"' : '';
						echo '<option value="'.$domain['id'].'"'.$selected.'>'.stripslashes($domain['name']).'</option>'."\n\t";
					}
					echo '</select>'."\n";
				
				// create language dropdown
					$changeJS = "window.location='".$cosa->system('base-url')."/?m=".$m."&s=".$s."&l='+this.value;";
					echo '<select id="l_dd" name="l" onchange="'.$changeJS.'">'."\n\t";
					foreach($this->_data[$this->domain_id]['languages'] as $k => $language) {
						$selected = ($this->id == $language['id']) ? ' selected="selected"' : '';
						echo '<option value="'.$language['id'].'"'.$selected.'>'.stripslashes($language['name']).'</option>'."\n\t";
					}
					echo '</select>'."\n";
				
			}
		
	}
	
	
	/**
	 * Get languages by usergroup
	 * 
	 * @param	int		$group_id
	 * @return	array
	 */
	
	public function getUserLanguages($group_id=false)
	{
		global $user;
		return $user->getLanguages($group_id);
	}
	
	
	/**
	 * Get all available languages/domains
	 * 
	 * @return	array
	 */
	
	public function getAllLanguages()
	{
		return $this->getData(false, false);
	}
	
	
	/**
	 * Get language values from key
	 * 
	 * @param	string		$key
	 * @return	string
	 */
	
	public function get($key)
	{
		if(isset($this->_module[$key])) return $this->_module[$key];
		if(isset($this->_system[$key])) return $this->_system[$key];
		return '<!-- language var with key not found: '.$key.' -->';
	}
	
	
	/**
	 * Print language value from key
	 * 
	 * @param	string		$key
	 * @return	void
	 */
	
	public function put($key)
	{
		echo $this->get($key);
	}
	
	
	/**
	 * Set the module language array
	 * 
	 * @param	array	$var
	 * @return	bool
	 */
	
	public function addModuleLanguage($data)
	{
		global $cosa;
		if(!is_array($data)) $cosa->kill('Could not add module data.<br />Please check the module language file.');
		$this->_module = $data;
	}
	
	
	/**
	 * Set the system language array
	 * 
	 * @param	array	$var
	 * @return	bool
	 */
	
	public function addSystemLanguage($data)
	{
		global $cosa;
		if(!is_array($data)) $cosa->kill('Could not add system data.<br />Please check the system language file.');
		$this->_system = $data;
	}
	
	
	/**
	 * Get default language id
	 * 
	 * @return	int
	 */
	
	public function defaultId()
	{
		global $db;
		/*$sql = "SELECT l.id, l.name, l.short, d.id AS domain_id, d.name AS domain_name, d.short AS domain_short
				FROM ".$db->table('languages')." l, ".$db->table('domains')." d
				WHERE l.domain_id = d.id
				AND l.active = '1' AND d.active = '1'
				AND l.default = '1' AND d.default = '1'
				";*/
		$sql = "SELECT l.id, l.name, l.short, d.id AS domain_id, d.name AS domain_name, d.short AS domain_short
				FROM ".$db->table('languages')." l, ".$db->table('domains')." d
				WHERE l.domain_id = d.id
				AND l.default = '1' AND d.default = '1'
				";
		$res = mysql_query($sql) or die(mysql_error());
		if(!mysql_num_rows($res)) die('Could not fetch default language.');
		$row = mysql_fetch_array($res);
		return $row['id'];
	}
	
	
}


?>